home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / dte5_1.zip / HWIBMCGA.C < prev    next >
C/C++ Source or Header  |  1991-02-06  |  39KB  |  1,285 lines

  1. /*
  2.  * Written by Douglas Thomson (1989/1990)
  3.  *
  4.  * This source code is released into the public domain.
  5.  */
  6.  
  7. /*
  8.  * Name:    dte - Doug's Text Editor program - hardware dependent module
  9.  * Purpose: This file contains all the code that needs to be different on
  10.  *           different hardware.
  11.  * File:    hwibmcga.c
  12.  * Author:  Douglas Thomson
  13.  * System:  This particular version is for the IBM PC and close compatibles.
  14.  *           It uses Turbo C's screen output functions, which means there
  15.  *           should be no problem with "snow" on CGA cards. See the file
  16.  *           "hwibm.c" for a faster version that writes directly to the
  17.  *           video RAM.
  18.  *          This version also supports a command line option to simulate
  19.  *           slow baud rates, so that a PC user can get some idea of how
  20.  *           the editor would perform across a slow serial line. This
  21.  *           feature was probably more useful to me in designing screen
  22.  *           update algorithms, but may be of interest to others also...
  23.  * Date:    October 10, 1989
  24.  * Notes:   This module has been kept as small as possible, to facilitate
  25.  *           porting between different systems.
  26.  */
  27. #include "common.h"     /* dte types */
  28. #include "hwdep.h"      /* prototypes for functions here */
  29. #include "utils.h"      /* for displaying messages etc */
  30. #include "version.h"    /* current version number */
  31. #include <stdarg.h>     /* for passing variable numbers of arguments */
  32. #include <conio.h>      /* for using putch to output a character */
  33. #include <dos.h>        /* for renaming files */
  34. #include <dir.h>        /* for searching the current path */
  35. #include <bios.h>       /* for direct BIOS keyboard input */
  36. #include <alloc.h>      /* for memory allocation */
  37. #include <io.h>         /* for file attribute code */
  38. #include <fcntl.h>      /* open flags */
  39. #include <process.h>    /* spawn etc */
  40. #include <sys/stat.h>   /* S_IWRITE etc */
  41.  
  42. /*
  43.  * prototypes for all functions in this file
  44.  */
  45. void error ARGS((int kind, ...));
  46. void main ARGS((int argc, char *argv[]));
  47. static void sim_delay ARGS((int n));
  48. static void att_stuff ARGS((void));
  49. void att_check ARGS((void));
  50. void hw_xygoto ARGS((void));
  51. int hw_clreol ARGS((void));
  52. int hw_linedel ARGS((int line));
  53. int hw_scroll_up ARGS((int top, int bottom));
  54. int hw_lineins ARGS((int line));
  55. int hw_scroll_down ARGS((int top, int bottom));
  56. int hw_c_avail ARGS((void));
  57. int hw_c_input ARGS((void));
  58. void hw_c_output ARGS((int c));
  59. void hw_terminate ARGS((void));
  60. void hw_initialize ARGS((void));
  61. void hw_move ARGS((text_ptr dest, text_ptr source, long number));
  62. int hw_backspace ARGS((void));
  63. int hw_c_insert ARGS((void));
  64. int hw_c_delete ARGS((void));
  65. int hw_rename ARGS((char *old, char *new));
  66. int hw_fattrib ARGS((char *name));
  67. int hw_set_fattrib ARGS((char *name, int attrib));
  68. int hw_unlink ARGS((char *name));
  69. int hw_printable ARGS((int c));
  70. int hw_load ARGS((char *name, text_ptr start, text_ptr limit, text_ptr *end));
  71. static int write_file ARGS((char *name, char *mode, text_ptr start,
  72.         text_ptr end));
  73. int hw_save ARGS((char *name, text_ptr start, text_ptr end));
  74. int hw_append ARGS((char *name, text_ptr start, text_ptr end));
  75. int hw_print ARGS((text_ptr start, text_ptr end));
  76. void hw_copy_path ARGS((char *old, char *name, char *new));
  77. int hw_os_shell ARGS((void));
  78.  
  79. /*
  80.  * Name:    error
  81.  * Purpose: To report an error, and usually make the user type <ESC> before
  82.  *           continuing.
  83.  * Date:    October 10, 1989
  84.  * Passed:  kind:   an indication of how serious the error was:
  85.  *                      TEMP:    merely a message, do not wait for <ESC>
  86.  *                      DIAG:    merely a message, but make sure user sees it
  87.  *                      WARNING: error, but editor can continue after <ESC>
  88.  *                      FATAL:   abort the editor!
  89.  *          format: printf format string for any arguments that follow
  90.  *          ...:    arguments to be printed
  91.  * Notes:   This function should be system independent; that is the whole
  92.  *           point of the "stdarg" philosophy. However, two of the systems
  93.  *           I have used implemented "stdarg" incompatibly, and some older
  94.  *           systems may not support the "stdarg" macros at all...
  95.  */
  96. void error(kind, format)
  97. int kind;
  98. char *format;
  99. {
  100.     va_list argptr;         /* used to access various arguments */
  101.     char buff[MAX_COLS];    /* somewhere to store error before printing */
  102.     int c;                  /* character entered by user to continue */
  103.  
  104.     /*
  105.      * prepare to process variable arguments
  106.      */
  107.     va_start(argptr, format);
  108.  
  109.     /*
  110.      * tell the user what kind of an error it is
  111.      */
  112.     switch (kind) {
  113.     case FATAL:
  114.         strcpy(buff, "Fatal error: ");
  115.         break;
  116.     case WARNING:
  117.         strcpy(buff, "Warning: ");
  118.         break;
  119.     case DIAG:
  120.     case TEMP:
  121.         strcpy(buff, "");
  122.         break;
  123.     }
  124.  
  125.     /*
  126.      * prepare the error message itself
  127.      */
  128.     vsprintf(buff + strlen(buff), format, argptr);
  129.     va_end(argptr);
  130.  
  131.     /*
  132.      * tell the user how to continue editing if necessary
  133.      */
  134.     if (kind == WARNING || kind == DIAG) {
  135.         strcat(buff, ": type <ESC>");
  136.     }
  137.  
  138.     /*
  139.      * output the error message
  140.      */
  141.     set_prompt(buff, 1);
  142.  
  143.     if (kind == FATAL) {
  144.         /*
  145.          * no point in making the user type <ESC>, since the program is
  146.          *  about to abort anyway...
  147.          */
  148.         terminate();
  149.         exit(1);
  150.     }
  151.     else if (kind != TEMP) {
  152.         /*
  153.          * If necessary, force the user to acknowledge the error by
  154.          *  typing <ESC> (or ^U).
  155.          * This prevents any extra commands the user has entered from
  156.          *  causing problems after an error may have made them inappropriate.
  157.          */
  158.         while ((c=c_input()) != 27 && c != CONTROL('U')) {
  159.             set_prompt(buff, 1);
  160.         }
  161.     }
  162. }
  163.  
  164. /*
  165.  * g_speed stores the number of milliseconds it takes to transmit a single
  166.  *  character across a simulated communication line.
  167.  * The default is to run at full speed. (My intuitive observation is that
  168.  *  on a 4.77 MHz IBM PC, "full speed" corresponds to about 4800 baud!)
  169.  */
  170. static int g_speed = 0;
  171.  
  172. /*
  173.  * Name:    harmless
  174.  * Purpose: To process control-break by ignoring it, so that the editor is
  175.  *           not aborted!
  176.  * Date:    February 5, 1990
  177.  */
  178. static int harmless(void)
  179. {
  180.     return 1;   /* ignore */
  181. }
  182.  
  183.  
  184. /*
  185.  * original control-break checking flag
  186.  */
  187. static int s_cbrk;
  188.  
  189. /*
  190.  * Name:    main
  191.  * Purpose: To do any system dependent command line argument processing,
  192.  *           and then call the main editor function.
  193.  * Date:    October 10, 1989
  194.  * Passed:  argc:   number of command line arguments
  195.  *          argv:   text of command line arguments
  196.  */
  197. void main(argc, argv)
  198. int argc;
  199. char *argv[];
  200. {
  201.     char drive[MAXDRIVE];  /* drive which dte.exe came from */
  202.     char dir[MAXDIR];      /* directory for dte.exe */
  203.  
  204.     /*
  205.      * trap control-break to make it harmless, and turn checking off
  206.      */
  207.     s_cbrk = getcbrk();
  208.     ctrlbrk(harmless);
  209.     setcbrk(0);
  210.  
  211.     /*
  212.      * set up help file name. This is a file called dte.hlp, and it should
  213.      *  be in the same directory as the dte.exe program.
  214.      * This information is only available in DOS 3 and later, so we need
  215.      *  to check to see whether argv[0] was OK.
  216.      */
  217.     if (fnsplit(argv[0], drive, dir, NULL, NULL) & DIRECTORY) {
  218.         fnmerge(g_status.help_file, drive, dir, "dte", ".hlp");
  219.     }
  220.  
  221.     /*
  222.      * see if the user specified a baud rate to simulate
  223.      */
  224.     if (*argv[1] == '-') {
  225.         /*
  226.          * most serial lines use about 10 bits per character (7 data, 1
  227.          *  parity, 1 start and 1 stop). Since I want milliseconds per
  228.          *  character, I divide 10000 by the baud rate.
  229.          */
  230.         g_speed = 10000 / atoi(&(argv[1][1]));
  231.  
  232.         /*
  233.          * On a standard PC, it already takes over 1 millisecond to
  234.          *  process a character, so adjust the rate accor